package com.meiya.lab.algorithm.ahocorasick;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;



/**
   Junit test cases for AhoCorasick.
 */

public class RunAhoCorasick {
	private AhoCorasick tree;

    public void setUp() {
    	this.tree = new AhoCorasick();
    }
    public RunAhoCorasick(){
    	setUp();
    }
	public static void main(String[] args) {
		new RunAhoCorasick().testTest();
	}
	public void testTest(){
		String text="<div class=\"th_abs\">小米无法下载移动的彩信，包括彩信，手机报等。移动技术人员也无法解决。求解，是系统问题吗？现在手机系统是miui2.6.16 网上说：在“流量监控”--“...<a class=\"th_reply\" href=\"/p/1680573612#sub\">快速回复</a> </div>";
		String[] terms = {
			    "miui"
			};
		
		long d1 = new Date().getTime();
		for (int i = 0; i < terms.length; i++) {
		    tree.add(terms[i].getBytes(), terms[i]);
		}
		tree.prepare();

		Set termsThatHit = new HashSet();
		for (Iterator iter = tree.search(text.getBytes()); iter.hasNext(); ) {
		    SearchResult result = (SearchResult) iter.next();
		    termsThatHit.addAll(result.getOutputs());
		}
		System.out.println(termsThatHit);
		long d2 = new Date().getTime();
		System.out.println("ac耗时："+(d2-d1));
	}
	
	public void largerTextExample() {
		String text = "The ga3 mutant of Arabidopsis is a gibberellin-responsive dwarf. We present data showing that the ga3-1 mutant is deficient in ent-kaurene oxidase activity, the first cytochrome P450-mediated step in the gibberellin biosynthetic pathway. By using a combination of conventional map-based cloning and random sequencing we identified a putative cytochrome P450 gene mapping to the same location as GA3. Relative to the progenitor line, two ga3 mutant alleles contained single base changes generating in-frame stop codons in the predicted amino acid sequence of the P450. A genomic clone spanning the P450 locus complemented the ga3-2 mutant. The deduced GA3 protein defines an additional class of cytochrome P450 enzymes. The GA3 gene was expressed in all tissues examined, RNA abundance being highest in inflorescence tissue.";
		String text1 = "GA3";
		String[] terms = {
		    "microsome",
		    "cytochrome",
		    "cytochrome P450 activity", 
		    "gibberellic acid biosynthesis", 
		    "GA3", 
		    "cytochrome P450", 
		    "oxygen binding", 
		    "AT5G25900.1", 
		    "protein", 
		    "RNA", 
		    "gibberellin", 
		    "Arabidopsis", 
		    "ent-kaurene oxidase activity", 
		    "inflorescence", 
		    "tissue", 
		};

		System.out.println("文本字符数："+text.length());
		System.out.println("关键词数：："+terms.length);
		long d1 = new Date().getTime();
		for (int i = 0; i < terms.length; i++) {
		    tree.add(terms[i].getBytes(), terms[i]);
		}
		tree.prepare();

		Set termsThatHit = new HashSet();
		for (Iterator iter = tree.search(text.getBytes()); iter.hasNext(); ) {
		    SearchResult result = (SearchResult) iter.next();
		    termsThatHit.addAll(result.getOutputs());
		}
		System.out.println(termsThatHit);
		long d2 = new Date().getTime();
		System.out.println("ac耗时："+(d2-d1));
		
		long d3 = new Date().getTime();

		
		Set termsThatHit1 = new HashSet();
		for (Iterator iter = tree.search(text1.getBytes()); iter.hasNext(); ) {
		    SearchResult result = (SearchResult) iter.next();
		    termsThatHit1.addAll(result.getOutputs());
		}
		System.out.println(termsThatHit1);
	}
}
